package com.zkc.blog.controller.admin;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkc.blog.pojo.Comment;
import com.zkc.blog.service.CommentService;
import com.zkc.blog.uilt.ResultEntity;

@RestController
public class CommentController {

	@Autowired
	private CommentService commentService;
	
	@GetMapping("/comment")
	public ResultEntity<PageInfo<Comment>> getCommentList(
			@RequestParam(value = "searchkey", defaultValue = "") String  searchkey,
			@RequestParam(value = "start", defaultValue = "0") int start,
			@RequestParam(value = "size", defaultValue = "10") int size,
			@RequestParam(value = "blogid",defaultValue = "-1") int blogid
			){
		PageHelper.startPage(start,size,"commentid desc");//这句要先启动
		List<Comment> list=commentService.getCommentList(searchkey,blogid);
		PageInfo<Comment> page = new PageInfo<>(list);//分页处理
		
		return ResultEntity.successWithoutData(page);
	}
	@GetMapping("/comment/{id}")
	public ResultEntity<Comment> getCommentById(
			@PathVariable("id") Integer  id
			){
		Comment comment=commentService.getCommentById(id); 
		
		return ResultEntity.successWithoutData(comment);
	}
	
	//增加
	@PostMapping("/comment")
	public ResultEntity<String> addComment(
			@RequestBody(required = true) Comment comment
			){
		try {
			comment.setCreatetime(new Date());
			comment.setUpdatetime(new Date());
			commentService.insertComment(comment);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	}
	
	//delete
	@DeleteMapping("/comment")
	public ResultEntity<String> deleteComments(
			@RequestBody  List<Integer> commentids
			){
		try {
			commentService.deleteCommentById(commentids);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	}
}
